package com.yuanfeng.activity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.activity.dto.ActivityExplainParamDTO;
import com.yuanfeng.activity.entity.ActivitySpinwinGiftEntity;
import com.yuanfeng.activity.mapper.ActivitySpinwinGiftMapper;
import com.yuanfeng.activity.service.ActivitySpinwinGiftService;
import com.yuanfeng.activity.vo.SpinwinGiftListVO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.StringUtils;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ActivitySpinwinGiftServiceImpl extends ServiceImpl<ActivitySpinwinGiftMapper, ActivitySpinwinGiftEntity>
    implements ActivitySpinwinGiftService{

    @Override
    public ResponseResult deleteSpinwinGift(Map<String, Object> param) {
        Integer count = this.baseMapper.selectGiftActivityCount(param);
        if (null != count && count.intValue() > 0) {
            return ResponseResult.fail(BizCodeEnume.REMOVE_THE_ASSOCIATED_ACTIVITY);
        }
        this.baseMapper.deleteById(param.get("giftId").toString());

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult updateSpinwinGift(Map<String, Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.get("token").toString());

        param.put("updateBy",userInfo.getUserRealname());
        param.put("updateDate",new Date());
        //如果红包id为空 修改当前礼包状态为非红包
        if(null == param.get("redpacketTId") || "".equals(param.get("redpacketTId")) || 0 == Integer.parseInt(param.get("redpacketTId").toString())){
            //礼品是否是红包1-是,2-否
            param.put("giftIsRedpacket",2);
        }
        if (StringUtils.isEmpty(param.get("giftId").toString())) {
            this.baseMapper.insertSpinwinGift(param);
        } else {
            this.baseMapper.updateSpinwinGift(param);
        }

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult querySpinwinGiftList(Map<String, Object> param) {
        IPage<SpinwinGiftListVO> page=this.baseMapper.selectSpinwinGiftList(new Query<SpinwinGiftListVO>().getPage(param),
                param);
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult querySpinWinGift(ActivityExplainParamDTO param) {
        List<SpinwinGiftListVO> spinWinGiftList = this.baseMapper.selectSpinWinGift(param);
        return ResponseResult.success(spinWinGiftList);
    }
}




